home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Main.bin / Long.java < prev    next >
Text File  |  1998-09-22  |  20KB  |  582 lines

  1. /*
  2.  * @(#)Long.java    1.34 98/07/07
  3.  *
  4.  * Copyright 1995-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  * 
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.lang;
  16.  
  17. /**
  18.  * The Long class wraps a value of the primitive type <code>long</code> 
  19.  * in an object. An object of type <code>Long</code> contains a single 
  20.  * field whose type is <code>long</code>. 
  21.  * <p>
  22.  * In addition, this class provides several methods for converting a 
  23.  * <code>long</code> to a <code>String</code> and a 
  24.  * <code>String</code> to a <code>long</code>, as well as other 
  25.  * constants and methods useful when dealing with a 
  26.  * <code>long</code>. 
  27.  *
  28.  * @author  Lee Boynton
  29.  * @author  Arthur van Hoff
  30.  * @version 1.34, 07/07/98
  31.  * @since   JDK1.0
  32.  */
  33. public final
  34. class Long extends Number {
  35.     /**
  36.      * The smallest value of type <code>long</code>. 
  37.      *
  38.      * @since   JDK1.0
  39.      */
  40.     public static final long MIN_VALUE = 0x8000000000000000L;
  41.  
  42.     /**
  43.      * The largest value of type <code>long</code>. 
  44.      *
  45.      * @since   JDK1.0
  46.      */
  47.     public static final long MAX_VALUE = 0x7fffffffffffffffL;
  48.  
  49.     /**
  50.      * The Class object representing the primitive type long.
  51.      *
  52.      * @since   JDK1.1
  53.      */
  54.     public static final Class    TYPE = Class.getPrimitiveClass("long");
  55.  
  56.     /**
  57.      * Creates a string representation of the first argument in the 
  58.      * radix specified by the second argument. 
  59.      * <p>
  60.      * If the radix is smaller than <code>Character.MIN_RADIX</code> or 
  61.      * larger than <code>Character.MAX_RADIX</code>, then the radix 
  62.      * <code>10</code> is used instead. 
  63.      * <p>
  64.      * If the first argument is negative, the first element of the 
  65.      * result is the ASCII minus sign <code>'-'</code>. If the first 
  66.      * argument is not negative, no sign character appears in the result. 
  67.      * The following ASCII characters are used as digits: 
  68.      * <ul><code>
  69.      *   0123456789abcdefghijklmnopqrstuvwxyz
  70.      * </code></ul>
  71.      *
  72.      * @param   i       a long.
  73.      * @param   radix   the radix.
  74.      * @return  a string representation of the argument in the specified radix.
  75.      * @see     java.lang.Character#MAX_RADIX
  76.      * @see     java.lang.Character#MIN_RADIX
  77.      * @since   JDK1.0
  78.      */
  79.     public static String toString(long i, int radix) {
  80.         if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX)
  81.         radix = 10;
  82.     
  83.     char[] buf = new char[65];
  84.     int charPos = 64;
  85.     boolean negative = (i < 0);
  86.        
  87.     if (!negative) {
  88.         i = -i;
  89.     }
  90.     
  91.     while (i <= -radix) {
  92.         buf[charPos--] = Integer.digits[(int)(-(i % radix))];
  93.         i = i / radix;
  94.     }
  95.     buf[charPos] = Integer.digits[(int)(-i)];
  96.     
  97.     if (negative) {
  98.         buf[--charPos] = '-';
  99.     }
  100.  
  101.     return new String(buf, charPos, (65 - charPos));
  102.     }
  103.  
  104.     /**
  105.      * Creates a string representation of the long argument as an 
  106.      * unsigned integer in base 16. 
  107.      * <p>
  108.      * The unsigned long value is the argument plus 2<sup>64</sup> if 
  109.      * the argument is negative; otherwise, it is equal to the argument. 
  110.      * This value is converted to a string of ASCII digits in hexadecimal 
  111.      * (base 16) with no extra leading <code>0</code>s. 
  112.      *
  113.      * @param   i   a <code>long</code>.
  114.      * @return  the string representation of the unsigned long value
  115.      *          represented by the argument in hexadecimal (base 16).
  116.      * @since   JDK 1.0.2
  117.      */
  118.     public static String toHexString(long i) {
  119.     return toUnsignedString(i, 4);
  120.     }
  121.  
  122.     /**
  123.      * Creates a string representation of the long argument as an 
  124.      * unsigned integer in base 8. 
  125.      * <p>
  126.      * The unsigned long value is the argument plus 2<sup>64</sup> if 
  127.      * the argument is negative; otherwise, it is equal to the argument. 
  128.      * This value is converted to a string of ASCII digits in octal 
  129.      * (base 8) with no extra leading <code>0</code>s. 
  130.      *
  131.      * @param   i   a <code>long</code>.
  132.      * @return  the string representation of the unsigned long value
  133.      *          represented by the argument in octal (base 8).
  134.      * @since   JDK 1.0.2
  135.      */
  136.     public static String toOctalString(long i) {
  137.     return toUnsignedString(i, 3);
  138.     }
  139.  
  140.     /**
  141.      * Creates a string representation of the long argument as an 
  142.      * unsigned integer in base 2. 
  143.      * <p>
  144.      * The unsigned long value is the argument plus 2<sup>64</sup> if 
  145.      * the argument is negative; otherwise, it is equal to the argument. 
  146.      * This value is converted to a string of ASCII digits in binary 
  147.      * (base 2) with no extra leading <code>0</code>s. 
  148.      *
  149.      * @param   i   a long.
  150.      * @return  the string representation of the unsigned long value
  151.      *          represented by the argument in binary (base 2).
  152.      * @since   JDK 1.0.2
  153.      */
  154.     public static String toBinaryString(long i) {
  155.     return toUnsignedString(i, 1);
  156.     }
  157.  
  158.     /** 
  159.      * Convert the integer to an unsigned number.
  160.      */
  161.     private static String toUnsignedString(long i, int shift) {
  162.     char[] buf = new char[64];
  163.     int charPos = 64;
  164.     int radix = 1 << shift;
  165.     long mask = radix - 1;
  166.     do {
  167.         buf[--charPos] = Integer.digits[(int)(i & mask)];
  168.         i >>>= shift;
  169.     } while (i != 0);
  170.     return new String(buf, charPos, (64 - charPos));
  171.     }
  172.  
  173.     /**
  174.      * Returns a new String object representing the specified integer. The radix
  175.      * is assumed to be 10.
  176.      *
  177.      * @param   i   a <code>long</code> to be converted.
  178.      * @return  a string representation of the argument in base 10.
  179.      * @since   JDK1.0
  180.      */
  181.     public static String toString(long i) {
  182.     return toString(i, 10);
  183.     }
  184.  
  185.     /**
  186.      * Parses the string argument as a signed <code>long</code> in the 
  187.      * radix specified by the second argument. The characters in the 
  188.      * string must all be digits of the specified radix (as determined by 
  189.      * whether <code>Character.digit</code> returns a 
  190.      * nonnegative value), except that the first character may be an 
  191.      * ASCII minus sign <code>'-'</code> to indicate a negative value. 
  192.      * The resulting <code>long</code> value is returned. 
  193.      *
  194.      * @param      s       the <code>String</code> containing the
  195.      *                     <code>long</code>.
  196.      * @param      radix   the radix to be used.
  197.      * @return     the <code>long</code> represented by the string argument in
  198.      *             the specified radix.
  199.      * @exception  NumberFormatException  if the string does not contain a
  200.      *               parsable integer.
  201.      * @since      JDK1.0
  202.      */
  203.     public static long parseLong(String s, int radix) 
  204.               throws NumberFormatException 
  205.     {
  206.         if (s == null) {
  207.             throw new NumberFormatException("null");
  208.         }
  209.  
  210.     if (radix < Character.MIN_RADIX) {
  211.         throw new NumberFormatException("radix " + radix + 
  212.                         " less than Character.MIN_RADIX");
  213.     }
  214.     if (radix > Character.MAX_RADIX) {
  215.         throw new NumberFormatException("radix " + radix + 
  216.                         " greater than Character.MAX_RADIX");
  217.     }
  218.  
  219.     long result = 0;
  220.     boolean negative = false;
  221.     int i = 0, max = s.length();
  222.     long limit;
  223.     long multmin;
  224.     int digit;
  225.  
  226.     if (max > 0) {
  227.         if (s.charAt(0) == '-') {
  228.         negative = true;
  229.         limit = Long.MIN_VALUE;
  230.         i++;
  231.         } else {
  232.         limit = -Long.MAX_VALUE;
  233.         }
  234.         multmin = limit / radix;
  235.             if (i < max) {
  236.                 digit = Character.digit(s.charAt(i++),radix);
  237.         if (digit < 0) {
  238.             throw new NumberFormatException(s);
  239.         } else {
  240.             result = -digit;
  241.         }
  242.         }    
  243.         while (i < max) {
  244.         // Accumulating negatively avoids surprises near MAX_VALUE
  245.         digit = Character.digit(s.charAt(i++),radix);
  246.         if (digit < 0) {
  247.             throw new NumberFormatException(s);
  248.         }
  249.         if (result < multmin) {
  250.             throw new NumberFormatException(s);
  251.         }
  252.         result *= radix;
  253.         if (result < limit + digit) {
  254.             throw new NumberFormatException(s);
  255.         }
  256.         result -= digit;
  257.         }
  258.     } else {
  259.         throw new NumberFormatException(s);
  260.     }
  261.     if (negative) {
  262.         if (i > 1) {
  263.         return result;
  264.         } else {    /* Only got "-" */
  265.         throw new NumberFormatException(s);
  266.         }
  267.     } else {
  268.         return -result;
  269.     }
  270.     }
  271.  
  272.     /**
  273.      * Parses the string argument as a signed decimal <code>long</code>. 
  274.      * The characters in the string must all be decimal digits, except 
  275.      * that the first character may be an ASCII minus sign 
  276.      * <code>'-'</code> to indicate a negative value. 
  277.      *
  278.      * @param      s   a string.
  279.      * @return     the <code>long</code> represented by the argument in decimal.
  280.      * @exception  NumberFormatException  if the string does not contain a
  281.      *               parsable <code>long</code>.
  282.      * @since      JDK1.0
  283.      */
  284.     public static long parseLong(String s) throws NumberFormatException {
  285.     return parseLong(s, 10);
  286.     }
  287.  
  288.     /**
  289.      * Returns a new long object initialized to the value of the
  290.      * specified String. Throws an exception if the String cannot be
  291.      * parsed as a long.
  292.      *
  293.      * @param      s       the <code>String</code> containing the
  294.      *                     <code>long</code>.
  295.      * @param      radix   the radix to be used. 
  296.      * @return     a newly constructed <code>Long</code> initialized to the
  297.      *             value represented by the string argument in the specified
  298.      *             radix.
  299.      * @exception  NumberFormatException  If the <code>String</code> does not
  300.      *               contain a parsable <code>long</code>.
  301.      * @since      JDK1.0
  302.      */
  303.     public static Long valueOf(String s, int radix) throws NumberFormatException {
  304.     return new Long(parseLong(s, radix));
  305.     }
  306.  
  307.     /**
  308.      * Returns a new long object initialized to the value of the
  309.      * specified String. Throws an exception if the String cannot be
  310.      * parsed as a long. The radix is assumed to be 10.
  311.      *
  312.      * @param      s   the string to be parsed.
  313.      * @return     a newly constructed <code>Long</code> initialized to the
  314.      *             value represented by the string argument.
  315.      * @exception  NumberFormatException  If the <code>String</code> does not
  316.      *               contain a parsable <code>long</code>.
  317.      * @since   JDK1.0
  318.      */
  319.     public static Long valueOf(String s) throws NumberFormatException 
  320.     {
  321.     return new Long(parseLong(s, 10));
  322.     }
  323.  
  324.     /**
  325.      * The value of the Long.
  326.      */
  327.     private long value;
  328.  
  329.     /**
  330.      * Constructs a newly allocated <code>Long</code> object that 
  331.      * represents the primitive <code>long</code> argument. 
  332.      *
  333.      * @param   value   the value to be represented by the <code>Long</code>.
  334.      * @since   JDK1.0
  335.      */
  336.     public Long(long value) {
  337.     this.value = value;
  338.     }
  339.  
  340.     /**
  341.      * Constructs a newly allocated <code>Long</code> object that 
  342.      * represents the value represented by the string. The string is 
  343.      * converted to an <code>long</code> value as if by the 
  344.      * <code>valueOf</code> method. 
  345.      *
  346.      * @param      s   the string to be converted to a <code>Long</code>.
  347.      * @exception  NumberFormatException  if the <code>String</code> does not
  348.      *               contain a parsable long integer.
  349.      * @see        java.lang.Long#valueOf(java.lang.String)
  350.      * @since      JDK1.0
  351.      */
  352.     public Long(String s) throws NumberFormatException {
  353.     this.value = parseLong(s, 10);
  354.     }
  355.  
  356.     /**
  357.      * Returns the value of this Long as a byte.
  358.      *
  359.      * @since   JDK1.1
  360.      */
  361.     public byte byteValue() {
  362.     return (byte)value;
  363.     }
  364.  
  365.     /**
  366.      * Returns the value of this Long as a short.
  367.      *
  368.      * @since   JDK1.1
  369.      */
  370.     public short shortValue() {
  371.     return (short)value;
  372.     }
  373.  
  374.     /**
  375.      * Returns the value of this Long as an int.
  376.      *
  377.      * @return  the <code>long</code> value represented by this object is
  378.      *          converted to type <code>int</code> and the result of the
  379.      *          conversion is returned.
  380.      * @since   JDK1.0
  381.      */
  382.     public int intValue() {
  383.     return (int)value;
  384.     }
  385.  
  386.     /**
  387.      * Returns the value of this Long as a long.
  388.      *
  389.      * @return  the <code>long</code> value represented by this object.
  390.      * @since   JDK1.0
  391.      */
  392.     public long longValue() {
  393.     return (long)value;
  394.     }
  395.  
  396.     /**
  397.      * Returns the value of this Long as a float.
  398.      *
  399.      * @return  the <code>long</code> value represented by this object is
  400.      *          converted to type <code>float</code> and the result of
  401.      *          the conversion is returned.
  402.      * @since   JDK1.0
  403.      */
  404.     public float floatValue() {
  405.     return (float)value;
  406.     }
  407.  
  408.     /**
  409.      * Returns the value of this Long as a double.
  410.      *
  411.      * @return  the <code>long</code> value represented by this object that
  412.      *          is converted to type <code>double</code> and the result of
  413.      *          the conversion is returned.
  414.      * @since   JDK1.0
  415.      */
  416.     public double doubleValue() {
  417.     return (double)value;
  418.     }
  419.  
  420.     /**
  421.      * Returns a String object representing this Long's value.
  422.      *
  423.      * @return  a string representation of this object in base 10.
  424.      * @since   JDK1.0
  425.      */
  426.     public String toString() {
  427.     return String.valueOf(value);
  428.     }
  429.  
  430.     /**
  431.      * Computes a hashcode for this Long.
  432.      *
  433.      * @return  a hash code value for this object. 
  434.      * @since   JDK1.0
  435.      */
  436.     public int hashCode() {
  437.     return (int)(value ^ (value >> 32));
  438.     }
  439.  
  440.     /**
  441.      * Compares this object against the specified object.
  442.      * The result is <code>true</code> if and only if the argument is 
  443.      * not <code>null</code> and is a <code>Long</code> object that 
  444.      * contains the same <code>long</code> value as this object. 
  445.      *
  446.      * @param   obj   the object to compare with.
  447.      * @return  <code>true</code> if the objects are the same;
  448.      *          <code>false</code> otherwise.
  449.      * @since   JDK1.0
  450.      */
  451.     public boolean equals(Object obj) {
  452.     if ((obj != null) && (obj instanceof Long)) {
  453.         return value == ((Long)obj).longValue();
  454.     }
  455.     return false;
  456.     }
  457.  
  458.     /**
  459.      * Determines the <code>long</code> value of the system property 
  460.      * with the specified name. 
  461.      * <p>
  462.      * The first argument is treated as the name of a system property. 
  463.      * System properties are accessible through <code>getProperty</code>  
  464.      * and , a method defined by the <code>System</code> class. The 
  465.      * string value of this property is then interpreted as a long value 
  466.      * and a <code>Long</code> object representing this value is returned. 
  467.      * Details of possible numeric formats can be found with the 
  468.      * definition of <code>getProperty</code>. 
  469.      * <p>
  470.      * If there is no property with the specified name, or if the 
  471.      * property does not have the correct numeric format, then 
  472.      * <code>null</code> is returned. 
  473.      *
  474.      * @param   nm   property name.
  475.      * @return  the <code>Long</code> value of the property.
  476.      * @see     java.lang.System#getProperty(java.lang.String)
  477.      * @see     java.lang.System#getProperty(java.lang.String, java.lang.String)
  478.      * @since   JDK1.0
  479.      */
  480.     public static Long getLong(String nm) {
  481.     SecurityManager sm = System.getSecurityManager();
  482.     if (sm != null)
  483.         sm.checkPropertyAccess(nm);
  484.     return getLong(nm, null);
  485.     }
  486.  
  487.     /**
  488.      * Determines the <code>long</code> value of the system property 
  489.      * with the specified name. 
  490.      * <p>
  491.      * The first argument is treated as the name of a system property. 
  492.      * System properties are accessible through <code>getProperty</code>  
  493.      * and , a method defined by the <code>System</code> class. The 
  494.      * string value of this property is then interpreted as a long value 
  495.      * and a <code>Long</code> object representing this value is returned. 
  496.      * Details of possible numeric formats can be found with the 
  497.      * definition of <code>getProperty</code>. 
  498.      * <p>
  499.      * If there is no property with the specified name, or if the 
  500.      * property does not have the correct numeric format, then a 
  501.      * <code>Long</code> object that represents the value of the second 
  502.      * argument is returned. 
  503.      *
  504.      * @param   nm    property name.
  505.      * @param   val   default value.
  506.      * @return  the <code>Long</code> value of the property.
  507.      * @see     java.lang.System#getProperty(java.lang.String)
  508.      * @see     java.lang.System#getProperty(java.lang.String, java.lang.String)
  509.      * @since   JDK1.0
  510.      */
  511.     public static Long getLong(String nm, long val) {
  512.     SecurityManager sm = System.getSecurityManager();
  513.     if (sm != null)
  514.         sm.checkPropertyAccess(nm);
  515.         Long result = Long.getLong(nm, null);
  516.         return (result == null) ? new Long(val) : result;
  517.     }
  518.  
  519.     /**
  520.      * Determines the <code>long</code> value of the system property 
  521.      * with the specified name. 
  522.      * <p>
  523.      * The first argument is treated as the name of a system property. 
  524.      * System properties are accessible through <code>getProperty</code>  
  525.      * and , a method defined by the <code>System</code> class. The 
  526.      * string value of this property is then interpreted as a long value 
  527.      * and a <code>Long</code> object representing this value is returned. 
  528.      * <p>
  529.      * If the property value begins with "<code>0x</code>" or 
  530.      * "<code>#</code>", not followed by a minus sign, the rest 
  531.      * of it is parsed as a hexadecimal integer exactly as for the method 
  532.      * <code>Long.valueOf</code> with radix 16. 
  533.      * <p>
  534.      * If the property value begins with "<code>0</code>", 
  535.      * then it is parsed as an octal integer exactly as for the method 
  536.      * <code>Long.valueOf</code> with radix 8. 
  537.      * <p>
  538.      * Otherwise the property value is parsed as a decimal integer 
  539.      * exactly as for the method <code>Long.valueOf</code> with radix 10. 
  540.      * <p>
  541.      * Note that, in every case, neither <code>L</code> nor 
  542.      * <code>l</code> is permitted to appear at the end of the string. 
  543.      * <p>
  544.      * The second argument is the default value. If there is no property 
  545.      * of the specified name, or if the property does not have the 
  546.      * correct numeric format, then the second argument is returned. 
  547.      *
  548.      * @param   nm    the property name.
  549.      * @param   val   the default <code>Long</code> value.
  550.      * @return  the <code>long</code> value of the property.
  551.      * @see     java.lang.Long#valueOf(java.lang.String, int)
  552.      * @see     java.lang.System#getProperty(java.lang.String)
  553.      * @see     java.lang.System#getProperty(java.lang.String, java.lang.String)
  554.      * @since   JDK1.0
  555.      */
  556.     public static Long getLong(String nm, Long val) {
  557.     SecurityManager sm = System.getSecurityManager();
  558.     if (sm != null)
  559.         sm.checkPropertyAccess(nm);
  560.     String v = System.getProperty(nm);
  561.     if (v != null) {
  562.         try {
  563.         if (v.startsWith("0x")) {
  564.             return Long.valueOf(v.substring(2), 16);
  565.         }
  566.         if (v.startsWith("#")) {
  567.             return Long.valueOf(v.substring(1), 16);
  568.         }
  569.         if (v.startsWith("0") && v.length() > 1) {
  570.             return Long.valueOf(v.substring(1), 8);
  571.         }
  572.         return Long.valueOf(v);
  573.         } catch (NumberFormatException e) {
  574.         }
  575.     }    
  576.     return val;
  577.     }
  578.  
  579.     /** use serialVersionUID from JDK 1.0.2 for interoperability */
  580.     private static final long serialVersionUID = 4290774380558885855L;
  581. }
  582.